package com.flexcloudsdk.fetch;

import java.util.logging.Logger;
import java.util.List;
import com.google.appengine.api.urlfetch.HTTPHeader;
import com.google.appengine.api.urlfetch.HTTPRequest;
import com.google.appengine.api.urlfetch.HTTPMethod;
import com.google.appengine.api.urlfetch.FetchOptions;

public class CloudHttpRequest {
	private static final Logger log = Logger.getLogger(CloudHttpRequest.class.getName());
	private  HTTPRequest request = null;
	private static final long serialVersionUID = 1L;
	private String sourceClass;
	private String sourceMethod;

	public CloudHttpRequest() {
		log.info("CloudHttpRequest has been created by BlazeDS");
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		sourceClass = CloudHttpRequest.class.getName();
	}
	
	public CloudHttpRequest(HTTPRequest value) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		request = value;	
	}
	
	public void setRequest(HTTPRequest value) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		request = value;
	}
	
	public HTTPRequest getRequest() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);
		return request;
	}
	
	public void addHeader(HTTPHeader header) {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		if (request != null)
			request.addHeader(header);
		else
			log.warning("Internal HTTPRequest has not been set before addHeader(header) call");
	}
	
	public FetchOptions getFetchOptions() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		return request.getFetchOptions();
	}
	
	public List<HTTPHeader> getHeaders() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		return request.getHeaders();
	}
	
	public HTTPMethod getMethod() {
		sourceMethod = Thread.currentThread().getStackTrace()[1].getMethodName().toString();
		log.info("Executing: " + sourceClass + "." + sourceMethod);

		return request.getMethod();
	}
	
}
